package chucnang;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import net.miginfocom.swing.MigLayout;

import com.toedter.calendar.JDateChooser;

import form.DSJ_XuatHang;

import trangchu.TrangChu;

public class DoanhThu extends JPanel implements ActionListener {

	private JPanel plNorth,plCenter,plSouth;
	private JButton btnXem,btnXemToanBo;
	private JTextField txtThanhToan;
	public JTable table;
	public DefaultTableModel model;
	public JDateChooser dateStart,dateEnd;
	private JLabel lblgoithieu;
	public DoanhThu(TrangChu trangchu) {
		
		String[][] data = {};
	    String[] colunms = {"Ngày","Tổng Doanh Thu","Ghi Chú"};
			this.setLayout(new BorderLayout());
			plNorth = new JPanel();
			plNorth.setForeground(Color.BLUE);
			plNorth.setBorder(new EmptyBorder(5, 5, 5, 5));
			add(plNorth,BorderLayout.NORTH);
			plNorth.setLayout(new MigLayout("", "[][][grow]", "[][]"));
			
			//this.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.blue), "Danh Sách Công Nợ"));
			this.setBorder(new TitledBorder("Doanh Thu Theo Ngày"));
			
			JLabel lblTu = new JLabel("Từ : ");
			plNorth.add(lblTu, "flowx,cell 0 1");
			
			dateStart = new JDateChooser();
			dateStart.setPreferredSize(new Dimension(180, 20));
			dateStart.setDateFormatString("dd/MM/yyyy");
			Date todayD=new Date(System.currentTimeMillis());
			dateStart.setDate(todayD);
			plNorth.add(dateStart, "cell 0 1,growx");
			
			JLabel lblDen = new JLabel("Đến : ");
			plNorth.add(lblDen, "cell 1 1,alignx trailing");
			
			dateEnd = new JDateChooser();
			dateEnd.setPreferredSize(new Dimension(180, 20));
			dateEnd.setDateFormatString("dd/MM/yyyy");
			Date todayD1=new Date(System.currentTimeMillis());
			dateEnd.setDate(todayD1);
			plNorth.add(dateEnd, "flowx,cell 2 1");
			
			btnXem = new JButton("Xem",new ImageIcon("images/xem1.gif"));
			plNorth.add(btnXem, "cell 2 1");
								
			
			plCenter = new JPanel();
			add(plCenter,BorderLayout.CENTER);
			plCenter.setLayout(new BorderLayout());
	    	JScrollPane scrollPane = new JScrollPane();
	    	plCenter.add(scrollPane);
			
	    	model = new DefaultTableModel(data, colunms);
			table = new JTable(model);
			table.getColumnModel().getColumn(0).setPreferredWidth(100);
			table.getColumnModel().getColumn(1).setPreferredWidth(200);
	    	table.getColumnModel().getColumn(2).setPreferredWidth(700);
			scrollPane.setViewportView(table);
			
			plSouth = new JPanel();
			add(plSouth,BorderLayout.SOUTH);
			plSouth.setLayout(new MigLayout("", "[][][][][][][][][][][][][][][][][][][][][][][grow][][grow][][][][][grow][grow][grow][grow]", "[][]"));	
			
			JLabel lblThanhToan = new JLabel("Tổng Số Tiền :");
			plSouth.add(lblThanhToan, "cell 32 0,alignx right");
			
			txtThanhToan = new JTextField();
			txtThanhToan.setEditable(false);
			txtThanhToan.setText("0");
			plSouth.add(txtThanhToan, "cell 32 0,alignx right");
			txtThanhToan.setColumns(15);
			
			lblgoithieu = new JLabel("Phần mềm quản lý bán hàng ShopM                  ");
			lblgoithieu.setFont(new Font("Tahoma", 0, 10));
			lblgoithieu.setFont(new Font("Tahoma", 3, 11));		
			lblgoithieu.setForeground(Color.blue);
			plSouth.add(lblgoithieu, "cell 32 1,alignx right");

			JPanel plSouthgio= new JPanel();
			plSouthgio.setLayout(new FlowLayout(FlowLayout.RIGHT));
			plSouth.add(plSouthgio, "cell 32 1,alignx right");
			final JLabel lblgioserver = new JLabel();
			plSouthgio.add(lblgioserver);
			 final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a | dd/MM/yyyy");
		        Timer time = new Timer(1000, new ActionListener()
		        {
		            public void actionPerformed(ActionEvent e)
		            {
		                Date d = new Date(System.currentTimeMillis());
		                lblgioserver.setText(sdf.format(d.getTime()));
		            }
		        });
		        time.start();		
		        //dang ki sukien
		        btnXem.addActionListener(this);

	}
	//so tien
	public void getInfoSoTien(){
		Integer a = 0;	
		for (int i = 0; i < table.getModel().getRowCount(); i++) {
			a = a +Integer.parseInt((String) table.getModel().getValueAt(i, 1));
		}
		txtThanhToan.setText(a.toString());	
	}
		//xem
	public void listxem(){
		DSJ_XuatHang b = new DSJ_XuatHang();
		Vector list = b.getDoanhThu(this);
		Vector list2;
		model.getDataVector().removeAllElements();
		for (int i = 0; i < list.size(); i++) {
			list2 = (Vector) list.get(i);
			Object[] oPerson = {
					list2.get(0).toString(),
					list2.get(1).toString(),};
			
			model.insertRow(0, oPerson);
			
		}
		model.fireTableDataChanged();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btnXem){
			listxem();
			getInfoSoTien();
		}
		
	}

}
